<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class SigninController extends MY_Controller {

	/**
	 */
	function __construct() {
		parent::__construct();
	}
	
	/**
	 * 
	 * */
	public function index() {
		//Validação apra redirecionar para profile caso esteja logado
		if(Auth::is_auth($this->session))
			redirect(url(URL_DEFAULT_LOGGED));
		
		if($this->is_post()) {
			//$error = new ErrorVo();
			//validação dos campos.
			$rules = array(
				array(
					'field' => 'user_email',
					'label' => '',
					'rules' => 'trim|xss_clean'
				),
				array(
					'field' => 'user_password',
					'label' => '',
					'rules' => 'trim|xss_clean'
				)
			);
			
			$validate = $this->form_validation->validate_data($rules);
			if( $validate[ 'status' ] ) {
				$form = $validate['data'];
				
				$retorno = $this->model->check_login($form['user_email'], $form['user_password']);
				
				if(count($retorno) > 0) {
					$user_data = $retorno[0];
					
					if($user_data['user_active']) {
						$permission = array();
						
						$set_session = array(
							'user_id' => $user_data['user_id'],
							'user_token' => $user_data['user_token'],
							'country_id' => $user_data['country_id'],
							'group_id' => $user_data['group_id'],
							'team_id' => $user_data['team_id'],
							'team_token' => $user_data['team_token'],
							'user_create_team' => $user_data['user_create_team'],
							'user_create_players' => $user_data['user_create_players'],
							'permissions' => $permission
						);
						
						//Cria sessão
						$this->session->set_userdata($set_session);
					
						//Redirect
						redirect(url(URL_DEFAULT_LOGGED));
					}
					else {//Usuário desativado
						$this->message_error = "Usuário desativado";	
					}
				}
				else {//Usuário não existe
					$this->message_error = "Usuário não existe";			
				}
			}
			else {//Erro no formulario
				$this->message_error = "Ocoreu algum erro Preencha novamente!!!";
			}
		}
				
		
		$this->set_data('title', PAGE_TITLE);
		$this->set_data('description', PAGE_DESCRIPTION);
		
		$this->load_view('signin/index');
	}

	/**
	 * */
	public function logout() {
		$this->session->sess_destroy();
		
		redirect(url(URL_DEFAULT_LOGIN));
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */